#include "CacheUpload.h"

CacheUpload::CacheUpload(QObject *parent):CacheBase(parent)
{

}

void CacheUpload::recvUploadInfoAndState(const QList<qlonglong> &id,const QList<QString> &info,const QList<QString> & status, const TransmitModule::SendInfoStatus &sendInfoStatus)
{
    if(sendInfoStatus == TransmitModule::SendInfoStatus::OK)
    {
        //
        deleteInfo(s_uploadInfoTableName,id);
    }
    else
    {
        deleteInfo(s_uploadInfoTableName,id);
        saveInfo(s_reuploadInfoTableName,info);
    }

}
void CacheUpload::recvReuploadInfoAndState(const QList<qlonglong> &id,const QList<QString> &info,const QList<QString> & status, const TransmitModule::SendInfoStatus & sendInfoStatus)
{
    if(sendInfoStatus == TransmitModule::SendInfoStatus::OK)
    {
        deleteInfo(s_reuploadInfoTableName,id);
    }
    else
    {
        updateInfo(s_reuploadInfoTableName,id,info,status);
    }
}

void CacheUpload::recvUploadSingal(qlonglong sendedInfoNum)
{
    QList<qlonglong> id;
    QList<QString> info;
    QList<QString> status;
    QString requestHead;

    readInfo(s_uploadInfoTableName,sendedInfoNum,id,info,status,requestHead);
    emit sendUploadInfo(id, info, status, requestHead);
}

void CacheUpload::recvReuploadSingal(qlonglong sendedInfoNum)
{
    QList<qlonglong> id;
    QList<QString> info;
    QList<QString> status;
    QString requestHead;
    readInfo(s_reuploadInfoTableName,sendedInfoNum,id,info,status,requestHead);
    emit sendReuploadInfo(id, info, status, requestHead);
}

bool CacheUpload::saveInfo(const QString &tableName, const QList<QString> &info)
{
    //	检查输入参数
    bool state = false;
    if (info.size()<=0)
    {
        state = false;
        return state;
    }
    QList<QString>  key;
    key << s_infoKey << s_stateKey;
    QList<QList<QVariant>> value;
    foreach(const QString &tempInfo,info)
    {
        QList<QVariant> tempValue;
        QString enInfoValue;
        QString statusInfoValue;
        QString enStatusInfoValue;
        // 生成加密信息字符串
        encryptSimple(tempInfo,enInfoValue);
        //	生成加密状态信息
        QJsonObject tempObj;
        tempObj.insert(s_createTimeStampKey,QDateTime::currentDateTime().toMSecsSinceEpoch()/1000);
        tempObj.insert(s_uploadTimesKey,0);
        statusInfoValue = QString(QJsonDocument(tempObj).toJson(QJsonDocument::JsonFormat::Compact));
        encryptSimple(statusInfoValue, enStatusInfoValue);
        tempValue << enInfoValue << enStatusInfoValue;
        value << tempValue;
    }
    state = m_dataBaseInterface->writeRow(tableName, key, value);
    return state;

}

bool CacheUpload::deleteInfo(const QString &tableName, const QList<qlonglong> &id)
{
    //	检查输入参数
    bool state = false;
    if (id.size()<=0)
    {
        state = false;
        return state;
    }
    QList<QString>  key;
    key << s_idKey;
    QList<QList<QVariant>> value;
    foreach(qlonglong tempInfo,id)
    {
        QList<QVariant> tempValue;
        tempValue << QVariant(tempInfo);
        value << tempValue;
    }
    state = m_dataBaseInterface->deleteRow(tableName, key, value);
    return state;
}

bool CacheUpload::updateInfo(const QString &tableName, const QList<qlonglong> &id, const QList<QString> &info, const QList<QString> &status)
{

    bool state = false;
    QList<QString> constraintKey;
    QList<QList<QVariant>> constraintValue;
    QList<QString> key;
    QList<QList<QVariant>> value;
    qlonglong nowTime = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000;
    constraintKey << s_idKey;
    key << s_infoKey << s_stateKey;
    for(qlonglong index=0;index < id.size();index++)
    {
        QList<QVariant> tempConstraintValue;
        QList<QVariant> tempValue;
        QString enInfo;
        QString enStatus;
        // 检查status信息
        QJsonDocument tempStatusJsonDoc = QJsonDocument::fromJson(status.at(index).toUtf8());
        if(!tempStatusJsonDoc.isObject())
        {
            continue;
        }
        QJsonObject tempStatusJsonObj = tempStatusJsonDoc.object();
        if(!tempStatusJsonObj.contains(s_uploadTimesKey))
        {
            continue;
        }
        tempStatusJsonObj[s_uploadTimesKey] = tempStatusJsonObj[s_uploadTimesKey].toVariant().toLongLong()+1 ;
        qDebug()<<"CacheUpload::updateInfo:tableName="<<tableName<<" index="<<index<<" times="<<tempStatusJsonObj;
        encryptSimple(info.at(index), enInfo);
        encryptSimple(QJsonDocument(tempStatusJsonObj).toJson(QJsonDocument::JsonFormat::Compact), enStatus);
        qlonglong tempId = id.at(index);
        tempConstraintValue << QVariant(tempId);
        tempValue << enInfo << enStatus;
        constraintValue << tempConstraintValue;
        value << tempValue;
    }
    state = m_dataBaseInterface->updateRow(tableName,constraintKey,constraintValue,key,value);
    qDebug()<<"CacheUpload::updateInfo:tableName="<<tableName<<" state="<<state;
    return state;
}

void CacheUpload::readInfo(const QString &tableName, qlonglong sendedInfoNum, QList<qlonglong> &id, QList<QString> &info, QList<QString> &status, QString &requestHead)
{
    QList<QString> uploadInfoKey;
    QList<QString> commonKey;
    uploadInfoKey << s_idKey << s_infoKey << s_stateKey;
    commonKey << s_infoKey;
    // 获取信息
    QList<QList<QVariant>> commonInfoList = m_dataBaseInterface->readRow(s_commonInfoTableName,commonKey,sendedInfoNum);
    if(commonInfoList.size() <= 0)
    {
        return;
    }
    QList<QList<QVariant>> uploadInfoList = m_dataBaseInterface->readRow(tableName,uploadInfoKey,sendedInfoNum);
    if(uploadInfoList.size() <= 0)
    {
        return;
    }
    //  封装信息
    decryptSimple(commonInfoList.first().first().toString(), requestHead);
    //
    for(int32_t indexNum=0; indexNum < uploadInfoList.size();indexNum++)
    {
        for(int32_t indexKey=0; indexKey < uploadInfoKey.size(); indexKey++)
        {
            if(uploadInfoKey.at(indexKey) == s_idKey)
            {
                id << uploadInfoList.at(indexNum).at(indexKey).toLongLong();
            }
            else if(uploadInfoKey.at(indexKey) == s_infoKey)
            {
                QString tempStr;
                decryptSimple(uploadInfoList.at(indexNum).at(indexKey).toString(), tempStr);
                info << tempStr;
            }
            else if(uploadInfoKey.at(indexKey) == s_stateKey)
            {
                QString tempStr;
                decryptSimple(uploadInfoList.at(indexNum).at(indexKey).toString(), tempStr);
                status << tempStr;
            }
        }
    }
}
